package ctn.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;

import org.jmesa.view.html.HtmlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import ctn.contant.MsgContant;
import ctn.contant.PlugContant;
import ctn.dao.inteface.CTNUserDao;
import ctn.dao.inteface.LinkEventDao;
import ctn.dao.inteface.MessageDao;
import ctn.event.CTNEvent;
import ctn.hibernate.po.CtnCpaSpec;
import ctn.hibernate.po.CtnLinkApprove;
import ctn.hibernate.po.CtnLinkEvent;
import ctn.hibernate.po.CtnMsgTempl;
import ctn.hibernate.po.CtnUser;
import ctn.hibernate.po.CtnUserMsg;
import ctn.hibernate.po.CtnUserMsgParm;
@Component
public class CPAUtil {

	@Autowired
	@Qualifier("filePro")
	private Properties filePro;
	@Autowired
	private LinkEventDao linkEventDaoImpl;
	@Autowired
	private MessageDao msgDao;
	@Autowired
	private CTNUserDao userDao;
	
	public void genMsg(CtnUserMsg msg){
		msg.setStatus(MsgContant.MESSAGE_STATUS_WAIT);
		msgDao.insertCtnUserMsg(msg);
	}
	public void genMsg(Long templID,Long sendUserID,Set<CtnUserMsgParm> paramters){
		CtnUser user=userDao.getUserByID(1L);
		CtnMsgTempl templ=msgDao.getMsgTemplByID(templID);
		CtnUserMsg msg=new CtnUserMsg();
		msg.setCtnMsgTempl(templ);
		msg.setCtnUser(user);
		msg.setSendUserId(sendUserID);
		for(CtnUserMsgParm parm:paramters){
			parm.setCtnUserMsg(msg);
		}
		msg.setCtnUserMsgParms(paramters);
		genMsg(msg);
	}
	public CtnLinkEvent genEvent(CTNEvent event){
		CtnLinkEvent cle=new CtnLinkEvent();
		cle.setEventLink(event.toEventLink());
		cle.setDesc(event.getEventID());
		cle.setUpdateDate(new Date());
		cle.setEventVal(event.getEventID());
		CtnLinkEvent rtnCle=linkEventDaoImpl.insertCtnLinkEvent(cle);
		return rtnCle;
	}
	public String genLink(String userID,CtnLinkEvent rtnCle) {
		StringBuffer sb = new StringBuffer();
		sb.append(filePro.getProperty(PlugContant.LINK_CONTEXT));
		sb.append("?").append(PlugContant.LINK_PARM_USER).append("=");
		sb.append(userID);
		sb.append("&").append(PlugContant.LINK_PARM_UUID).append("=");
		String uuid=CPAUtil.getUUID();
		sb.append(uuid);
		String link = sb.toString();
		
		CtnLinkApprove cla=new CtnLinkApprove();
		cla.setId(uuid);
		cla.setStatus(PlugContant.Link_APPROVE_STATUS_APPROVE);
		cla.setUserId(Integer.parseInt(userID));
		cla.setCtnLinkEvent(rtnCle);
		cla.setUpdateDate(new Date());
		linkEventDaoImpl.insertCtnLinkApprove(cla);
		try {
			link = URLEncoder.encode(link, "utf-8");
			HtmlBuilder html=new HtmlBuilder();
			html.ahref(sb.toString(), link);
			
			return html.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		// 去掉“-”符号
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
				+ s.substring(19, 23) + s.substring(24);
	}
	public  String getSpecFilePath(CtnCpaSpec spec){
		StringBuffer path=new StringBuffer();
		String rootFilepath=filePro.getProperty("common.file.spec");
		path.append(rootFilepath).append(PlugContant.FILE_SEPARATE);
		path.append(spec.getCtnCpaVer().getCtnCpaInfo().getId()).append(PlugContant.FILE_SEPARATE);
		path.append(spec.getCtnCpaVer().getId()).append(PlugContant.FILE_SEPARATE);
		if(spec.getCpaFun()!=null){
			path.append(spec.getCpaFun().getId()).append(PlugContant.FILE_SEPARATE);
		}
		path.append(StringUtils.formatDate(spec.getCreateDate(),"yyyyMMdd")).append(PlugContant.FILE_SEPARATE);
		path.append(spec.getFileName());
		
		return path.toString();
	}
	

}
